<template>
	<view>
		<view class="title">密码登录</view>
		<view class="login_wrap">
				<u--form
						labelPosition="left"
						:model="formObj"
						:rules="rules"
						ref="uForm"
					>
						<u-form-item
								prop="userName"
								ref="item1"
						>
							<u--input
									placeholder="用户名"
									v-model="formObj.userName"									 
							></u--input>
						</u-form-item>
						<u-form-item
								
								prop="userPwd"
								ref="item1"
						>
							<u--input
							placeholder="密码"
							v-model="formObj.userPwd"			
									   
							></u--input>
						</u-form-item>
						<u-form-item>
							<u-button type="success" @click="toLogin">登录</u-button>
						</u-form-item>
						<u-form-item>
							<u-button type="primary"  @click="toReg">去注册</u-button>
						</u-form-item>
					</u--form>
		</view>
	</view>
</template>

<script>
	import {toLoginApi} from '@/api/common.js'
	import { md5 } from 'js-md5'; // 引入
	export default {
			data(){
				return {
					formObj:{
						userName:"",		//用户名
						userPwd	:""	,   //密码
					},
					rules:{
						userName:{
							type:'string',
							required: true,
							message: '请填写用户名',
							trigger: ['blur'],
						},
						userPwd:{
							type:'string',
							min: 3,
							max:10,
							message: '密码3-10位',
							trigger: ['blur'],
						}
					}
				}
			
		},
		methods:{
			toReg(){//去注册
				uni.navigateTo({
					url:"/pages/mine/reg"
				})
			},
			toLogin(){
				this.$refs.uForm.validate().then(async  ()=>{
					// 密码加密问题 e10adc3949ba59abbe56e057f20f883e
					let pass=md5(this.formObj.userPwd);
					console.log(pass,"&&&&&&&&&&")
						let res=await toLoginApi({
							userName:this.formObj.userName,
							userPwd:this.formObj.userPwd
						})
						console.log(res,"99999999999")
						if(res.success){
							uni.setStorageSync("user",res.data)
							uni.showToast({
								title:"添加成功"
							})
							uni.switchTab({
								url:"/pages/mine/index"
							})
						}
						
				})
			}
		}
	}
</script>

<style lang="scss">
	.title{
		font-size: 20px;
		margin-top: 20px;
	}
	.login_wrap{
		padding: 30px;
		box-sizing: border-box;
	}
</style>